import Vue from 'vue'
import Vuex from 'vuex'
import { setStorage, getStorage } from '../utils/storage.js'
Vue.use(Vuex)
const myTokenInfo = getStorage() || {} // 如果直接写getStorage()，如其不存在则会报错（undefined.token不存在报错）而空对象的token返回undefined不报错
export default new Vuex.Store({
  state: {
    tokenInfo: {
      refresh_token: myTokenInfo.refresh_token || '',
      token: myTokenInfo.token || ''
    }
  },
  mutations: {
    mSaveToken (state, newTokenInfo) {
      state.tokenInfo = newTokenInfo
      setStorage(newTokenInfo)
    }
  },
  getters: {
    isLogin (state) {
      // !!强制转换为布尔值
      return !!state.tokenInfo.token
    }
  },
  actions: {
  },
  modules: {
    userInfo: {
      state: {
        birthday: '',
        gender: 1,
        id: 1,
        mobile: '',
        name: '',
        photo: ''
      },
      mutations: {
        mSaveUserInfo (state, newUserInfo) {
          state.birthday = newUserInfo.data.data.birthday
          state.gender = newUserInfo.data.data.gender
          state.id = newUserInfo.data.data.id
          state.mobile = newUserInfo.data.data.mobile
          state.name = newUserInfo.data.data.name
          state.photo = newUserInfo.data.data.photo
        }
      }
    }
  }
})
